/**
 * @Author      : maolin
 * @Date        : 2021/3/27
 * @Version     : 1.0
 * @Description : 纺织品业务相关
 **/
// 转换系数，经过行业打磨与沉淀，一般不会变动。
const ratio = 0.9144;
// 主单位一般有3种 kg、m、码
const unitList = {
  "24": {
    id: 'KG',
    label: 'KG',
  },
  "25": {
    id: 'M',
    label: 'M',
  },
  "26": {
    id: 'Y',
    label: '码'
  },
};
// 计算方式
const computedModeList = {
  "20": {
    id: "M",
    label: "平方克重换算"
  },
  "10": {
    id: "Y",
    label: "码克重换算"
  }
};
// 物料形态
export const materialShapeList = {
  "108": {
    id: "PRODUCT",
    label: "成品"
  },
  "109": {
    id: "GREY_CLOTH",
    label: "坯布"
  }
};

function isNull(a) {
  return a === null;
}
/**
 * 计算需求数量
 * 码克重转米数或码数：
 * 码数(Y)= 重量(KG)/码克重(G/Y)*1000
 * 米数(M)=重量(KG)/码克重(G/Y)*1000/0.9144
 * 重量(KG)=米数(M)/0.9144*码克重/1000
 * 重量(KG)=码数(Y)*码克重/1000
 * @param sellNum           销售数量
 * @param sellUnit          销售单位
 * @param mainUnit          主单位
 * @param doorWidth         门幅（如果计算方式为码，公式中需要使用门幅）
 * @param gweight           克重（若计算方式为米，则克重代表米克重，若计算方式为码，则克重代替码克重）
 * @param materialShape     物料形态（若为成品，则使用成品作为克重，若为坯布，则使用坯布作为克重）
 * @param computedMode      计算方式（一般只有米、码两种方式）
 * @return number 需求数量
 */
export function getDemandNum({sellNum, sellUnit, mainUnit, doorWidth, gweight, materialShape, computedMode}) {
  if (sellUnit == mainUnit) {
    return sellNum;
  }
  if (computedMode === undefined) {
    return 0;
  }
  // 保存常量、以及校验不能为空
  const sellUnitId = unitList[sellUnit] ? unitList[sellUnit].id : null;
  if (isNull(sellUnitId)) {
    return 0;
  }
  const mainUnitId = unitList[mainUnit] ? unitList[mainUnit].id : null;
  if (isNull(mainUnitId)) {
    return 0;
  }
  const materialShapeId = materialShapeList[materialShape] ? materialShapeList[materialShape].id : null;
  if (isNull(materialShapeId)) {
    return 0;
  }
  const computedModeId = computedModeList[computedMode] ? computedModeList[computedMode].id : null;

  let mgWeight = gweight;
  if (isNull(mgWeight)) {
    return 0;
  }
  // 如果计算方式是M，需要带入门幅
  if (computedModeId === 'M') {
    return mCompute({ratio, sellNum, sellUnitId, mainUnitId, mgWeight, doorWidth});
  } else if (computedModeId === 'Y') {
    return yCompute({ratio, sellNum, sellUnitId, mainUnitId, mgWeight});
  } else {
    return 0;
  }
}

export function getUnit({unit}) {
  return unitList[unit] ? unitList[unit].label : '';
}
/**
 * 以米来计算
 * 平方克重转米数/码数计算公式：
 * 码数(Y)=重量(KG)/[(幅宽(CM)* 平方米克重/100*0.9144)]*1000
 * 米数(M)=重量(KG)/[(幅宽(CM)* 平方米克重/100)]*1000
 * 重量(KG)=米数(M)*幅宽(CM)*平方米克重/100000
 * 重量(KG)=码数(Y)*幅宽(CM)*平方米克重/100000*0.9144
 * @param ratio
 * @param sellNum
 * @param sellUnitId
 * @param mainUnitId
 * @param mgWeight
 * @param doorWidth
 */
function mCompute({ratio, sellNum, sellUnitId, mainUnitId, mgWeight, doorWidth}) {
  if (mainUnitId === 'KG' && sellUnitId === 'M') {
    return mFromMToKG({ratio, sellNum, mgWeight, doorWidth});
  } else if (mainUnitId === 'KG' && sellUnitId === 'Y') {
    return mFromYToKG({ratio, sellNum, mgWeight, doorWidth});
  } else if (mainUnitId === 'M' && sellUnitId === 'KG') {
    return mFromKGToM({ratio, sellNum, mgWeight, doorWidth});
  } else if (mainUnitId === 'M' && sellUnitId === 'Y') {
    return mFromYToM({ratio, sellNum, mgWeight, doorWidth});
  } else if (mainUnitId === 'Y' && sellUnitId === 'KG') {
    return mFromKGToY({ratio, sellNum, mgWeight, doorWidth});
  } else if (mainUnitId === 'Y' && sellUnitId === 'M') {
    return mFromMToY({ratio, sellNum, mgWeight, doorWidth});
  } else {
    return 0;
  }
}

/**
 * 米计算，米转公斤
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromMToKG({sellNum, mgWeight, doorWidth}) {
  return sellNum * doorWidth * mgWeight / 100000;
}

/**
 * 米计算，码转公斤
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromYToKG({ratio, sellNum, mgWeight, doorWidth}) {
  return sellNum * doorWidth * mgWeight / 100000 * ratio;
}

/**
 * 米计算，公斤转米
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromKGToM({sellNum, mgWeight, doorWidth}) {
  return (sellNum / (doorWidth * mgWeight / 100)) * 1000;
}

/**
 * 米计算，码转米
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromYToM({ratio, sellNum, mgWeight, doorWidth}) {
  let kgNum = mFromYToKG({ratio, sellNum, mgWeight, doorWidth});
  return mFromKGToM({ratio, sellNum: kgNum, mgWeight, doorWidth});
}

/**
 * 米计算，公斤转码
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromKGToY({ratio, sellNum, mgWeight, doorWidth}) {
  return sellNum / (doorWidth * mgWeight / 100 * ratio) * 1000;
}

/**
 * 米计算，米转码
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function mFromMToY({ratio, sellNum, mgWeight, doorWidth}) {
  let num = mFromMToKG({ratio, sellNum, mgWeight, doorWidth});
  return mFromKGToY({ratio, sellNum: num, mgWeight, doorWidth});
}
/**
 * 以码来计算
 * 码克重转米数或码数：
 * 码数(Y)= 重量(KG)/码克重(G/Y)*1000
 * 米数(M)=重量(KG)/码克重(G/Y)*1000/0.9144
 * 重量(KG)=米数(M)/0.9144*码克重/1000
 * 重量(KG)=码数(Y)*码克重/1000
 * @param ratio
 * @param sellNum
 * @param sellUnitId
 * @param mainUnitId
 * @param mgWeight
 */
function yCompute({ratio, sellNum, sellUnitId, mainUnitId, mgWeight}) {
  if (mainUnitId === 'KG' && sellUnitId === 'M') {
    return yFromMToKG({ratio, sellNum, mgWeight});
  } else if (mainUnitId === 'KG' && sellUnitId === 'Y') {
    return yFromYToKG({ratio, sellNum, mgWeight});
  } else if (mainUnitId === 'M' && sellUnitId === 'KG') {
    return yFromKGToM({ratio, sellNum, mgWeight});
  } else if (mainUnitId === 'M' && sellUnitId === 'Y') {
    return yFromYToM({ratio, sellNum, mgWeight});
  } else if (mainUnitId === 'Y' && sellUnitId === 'KG') {
    return yFromKGToY({ratio, sellNum, mgWeight});
  } else if (mainUnitId === 'Y' && sellUnitId === 'M') {
    return yFromMToY({ratio, sellNum, mgWeight});
  } else {
    return 0;
  }
}

/**
 * 码计算，米转公斤
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @returns {number}
 */
function yFromMToKG({ratio, sellNum, mgWeight}) {
  return sellNum / ratio * mgWeight / 1000;
}

/**
 * 码计算，码转公斤
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @returns {number}
 */
function yFromYToKG({ratio, sellNum, mgWeight}) {
  return sellNum * mgWeight / 1000;
}

/**
 * 码计算，公斤转米
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function yFromKGToM({ratio, sellNum, mgWeight, doorWidth}) {
  return sellNum / mgWeight * 1000 / ratio;
}

/**
 * 码计算，码转米
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @returns {number}
 */
function yFromYToM({ratio, sellNum, mgWeight}) {
  let num = yFromYToKG({ratio, sellNum, mgWeight});
  return yFromKGToM({ratio, sellNum: num, mgWeight});
}

/**
 * 码计算，公斤转码
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @returns {number}
 */
function yFromKGToY({ratio, sellNum, mgWeight}) {
  return sellNum / mgWeight * 1000;
}

/**
 * 码计算，米转码
 * @param ratio
 * @param sellNum
 * @param mgWeight
 * @param doorWidth
 * @returns {number}
 */
function yFromMToY({ratio, sellNum, mgWeight}) {
  let num = yFromMToKG({ratio, sellNum, mgWeight});
  return yFromKGToY({ratio, sellNum: num, mgWeight});
}
